package com.farm.device.controller;

import com.farm.device.pojo.DeviceInfo;
import com.farm.device.service.DeviceInfoService;
import com.farm.device.service.DeviceService;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.device.pojo.Device;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 后台de设备controller
 */
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceInfoService deviceInfoService;



    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List <Device> all = deviceService.findAll();

            return new Result(true, StatusCode.OK,"查询成功",all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        try {
            Device byId = deviceService.findById(id);
            return new Result(true, StatusCode.OK,"查询成功",byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }


    @GetMapping("/findByName/{name}")
    public Result findByName(Device device){
        List <Device> byName = deviceService.findByName(device);
        if (byName.size()<=0||byName==null){
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"查询成功",byName);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try {

            deviceService.delete(id);
            deviceInfoService.delete(id);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Device device){

        try {
            deviceService.add(device);
            return new Result(true,StatusCode.OK,"添加成功了");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,StatusCode.ERROR,"添加失败了");
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody Device device){
        try {
            deviceService.update(device);

            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败了");
        }
    }

    @GetMapping("/findPage")
    public Result findPage(@RequestParam Map map,@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                           @RequestParam (name = "size",required = false,defaultValue = "10") Integer size){
        try {
            Page pageInfo = deviceService.findPage(map, page, size);
            PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }


}
